#!/usr/bin/evn python
# coding=utf-8
from typing import List, Dict

from peewee import fn

from infirmary_admin_src.infirmary_smart_admin.infirmary_models.admin_dbhelper import session_scope, session_scope_atomic
from infirmary_admin_src.infirmary_smart_admin.infirmary_models.admin_model import SysDictKey,SysDictValue


class SupportBusinesses:
    pass

    @classmethod
    def get_dict_key_id_max_id(cls):
        '''
        添加职务
        :param role_id:
        :return:
        '''
        with session_scope():
            return SysDictKey.select(fn.MAX(SysDictKey.dict_key_id)).scalar()


    @classmethod
    def edit_dict_key_batch(cls, ids=None, **kwargs):
        with session_scope():
            print("输出IDs：", ids)
            return SysDictKey.update(**kwargs).where(SysDictKey.dict_key_id.in_(ids)).execute()


    @classmethod
    def edit_dict_key_singe(cls, dict_key_id=None, **kwargs):
        with session_scope():
            return SysDictKey.update(**kwargs).where(SysDictKey.dict_key_id == dict_key_id).execute()

    @classmethod
    def get_dict_value_id_max_id(cls):
        '''
        添加职务
        :param role_id:
        :return:
        '''
        with session_scope():
            return SysDictValue.select(fn.MAX(SysDictValue.dict_value_id)).scalar()

    @classmethod
    def edit_dict_value_singe(cls, dict_key_id=None,dict_value_id=None, **kwargs):
        with session_scope():
            return SysDictValue.update(**kwargs).where(SysDictValue.dict_key_id == dict_key_id,
                                                       SysDictValue.dict_value_id == dict_value_id,
                                                       ).execute()

    @classmethod
    def edit_dict_value_batch(cls,ids=None, **kwargs):
        with session_scope():
            return SysDictValue.update(**kwargs).where(SysDictValue.dict_value_id.in_(ids)).execute()

    @classmethod
    def add_dict_value_singe(cls, **kwargs):
        '''
        添加职务
        :param role_id:
        :return:
        '''
        with session_scope():
            return SysDictValue.get_or_create(**kwargs)


    @classmethod
    def add_dict_key_singe(cls, **kwargs):
        '''
        添加职务
        :param role_id:
        :return:
        '''
        with session_scope():
            return SysDictKey.get_or_create(**kwargs)

    @staticmethod
    def get_dict_key_list(searchWord,pageNum = 1,pageSize = 10):
        with session_scope():
            # 获取所有的用户信息列表
            # 条件的表达式的串联
            def curr_expression():
                exprsopm = SysDictKey.deleted_flag == 0
                if searchWord:
                    exprsopm = (exprsopm & (SysDictKey.key_name.contains(searchWord))) if exprsopm else (
                        SysDictKey.key_name.contains(searchWord))

                return exprsopm

            _result_query = SysDictKey.select().where(curr_expression())
            _result_nums_total = _result_query.count()

            # 如果没有数据直接的不在进行分页查找
            if _result_nums_total > 0:
                _result = _result_query.paginate(pageNum, pageSize) \
                    .dicts() \
                    .order_by(SysDictKey.dict_key_id)

                if not _result:
                    # 把对应的结果保存到缓存中---缓存穿透：处理
                    return 0, []  # 查询没有结果的时候
                # 合并返回新的字典信息
                return _result_nums_total, [v for v in _result]
        return 0, None  # 查询没有结果的时候

    @staticmethod
    def get_dict_value_list(searchWord,dictKeyId,pageNum=1, pageSize=10):
        with session_scope():
            # 获取所有的用户信息列表
            # 条件的表达式的串联
            def curr_expression():
                exprsopm = SysDictValue.deleted_flag == 0
                exprsopm = (exprsopm & (SysDictValue.dict_key_id==dictKeyId)) if exprsopm else (
                    SysDictValue.dict_key_id==dictKeyId)

                if searchWord:
                    exprsopm = (exprsopm & (SysDictValue.value_name.contains(searchWord))) if exprsopm else (
                        SysDictValue.value_name.contains(searchWord))

                return exprsopm

            _result_query = SysDictValue.select().where(curr_expression())
            _result_nums_total = _result_query.count()

            # 如果没有数据直接的不在进行分页查找
            if _result_nums_total > 0:
                _result = _result_query.paginate(pageNum, pageSize) \
                    .dicts() \
                    .order_by(SysDictValue.sort.asc(),SysDictValue.dict_value_id)

                if not _result:
                    # 把对应的结果保存到缓存中---缓存穿透：处理
                    return 0, []  # 查询没有结果的时候
                # 合并返回新的字典信息
                return _result_nums_total, [v for v in _result]
        return 0, None  # 查询没有结果的时候
